package com.cjlgb.design.upms.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cjlgb.design.common.mybatis.bean.PageBean;
import com.cjlgb.design.common.core.bean.ResultBean;
import com.cjlgb.design.common.security.util.SpringSecurityUtil;
import com.cjlgb.design.upms.api.entity.SysUserInfo;
import com.cjlgb.design.upms.service.SysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.Optional;

/**
 * @author WFT
 * @date 2019/8/11
 * description: 后台用户控制器
 */
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/sys/user")
public class SysUserController {

    private final SysUserService userService;

    /**
     * 修改当前登录用户的信息
     * @param info 用户信息
     * @return ResultBean
     */
    @PutMapping(value = "/info")
    public ResultBean editInfo(@RequestBody SysUserInfo info){
        String loginName = SpringSecurityUtil.getLoginName();
        return this.userService.updateByAccountName(loginName, info);
    }


    /**
     * 获取当前登录用户的信息
     * @return ResultBean<SysUserInfo>
     */
    @GetMapping(value = "/info")
    public ResultBean<SysUserInfo> getMeInfo(){
        String loginName = SpringSecurityUtil.getLoginName();
        SysUserInfo userInfo = this.userService.selectByAccountName(loginName,null);
        Optional.ofNullable(userInfo)
                .ifPresent(bean -> {
                    //  用户权限列表
                    userInfo.setAuthorities(SpringSecurityUtil.getAuthorities());
                });
        return ResultBean.ok(userInfo);
    }

    /**
     * 分页查询用户信息
     * @param page 分页条件
     * @param params 查询参数
     * @return IPage<IPage>
     */
    @GetMapping(value = "/page")
    @PreAuthorize(value = "@pms.hasPermission('permission.user.query')")
    public ResultBean<IPage> pagingQuery(PageBean<SysUserInfo> page, SysUserInfo params){
        return ResultBean.ok(this.userService.pagingQuery(page,params));
    }

    /**
     * 添加用户
     * @param userInfo 用户信息
     * @return ResultBean
     */
    @PostMapping
    @PreAuthorize(value = "@pms.hasPermission('permission.user.add')")
    public ResultBean addUser(@RequestBody SysUserInfo userInfo){
        Assert.hasText(userInfo.getAccountName(),"登录名称不能为空");
        Assert.hasText(userInfo.getUserName(),"用户名称不能为空");
        Assert.notNull(userInfo.getSex(),"性别不能为空");
        Assert.notEmpty(userInfo.getRoleIds(),"用户角色不能为空");
        Assert.isTrue(1 == userInfo.getSex() || 2 == userInfo.getSex(),"参数异常");
        return this.userService.createUser(userInfo);
    }

    /**
     * 根据Id获取用户信息
     * @param accountId 账号Id
     * @return ResultBean<SysUserInfo>
     */
    @GetMapping(value = "/{accountId}")
    @PreAuthorize(value = "@pms.hasPermission('permission.user.edit')")
    public ResultBean<SysUserInfo> getById(@PathVariable(value = "accountId")Long accountId){
        return ResultBean.ok(this.userService.selectInfoByAccountId(accountId));
    }

    /**
     * 根据账号Id修改用户与角色的关联
     * @param userInfo 用户信息
     * @return ResultBean
     */
    @PutMapping
    @PreAuthorize(value = "@pms.hasPermission('permission.user.edit')")
    public ResultBean editUser(@RequestBody SysUserInfo userInfo){
        Assert.notNull(userInfo.getAccountId(),"账号Id不能为空");
        Assert.notEmpty(userInfo.getRoleIds(),"用户角色不能为空");
        return this.userService.updateByAccountId(userInfo);
    }


    /**
     * 修改账户的锁定状态
     * @param userInfo 账户信息
     * @return ResultBean
     */
    @PutMapping(value = "/status")
    @PreAuthorize(value = "@pms.hasPermission('permission.user.enable')")
    public ResultBean editLockFlag(@RequestBody SysUserInfo userInfo){
        Assert.notNull(userInfo.getId(),"Id不能为空");
        Assert.notNull(userInfo.getLockFlag(),"锁定标识不能为空");
        Assert.isTrue(0 == userInfo.getLockFlag() || 1 == userInfo.getLockFlag(),"参数异常");
        return this.userService.updateLockFlag(userInfo);
    }

    /**
     * 批量删除
     * @param accountIds Id列表
     * @return ResultBean
     */
    @PostMapping(value = "/batch/delete")
    @PreAuthorize(value = "@pms.hasPermission('permission.user.delete')")
    public ResultBean batchDelete(@RequestBody Collection<Long> accountIds){
        Assert.notEmpty(accountIds,"账号Id不能为空");
        this.userService.deleteByAccountIds(accountIds);
        return ResultBean.ok();
    }

}
